import http from '../utils/http';
import config from '../config';

export default {
  login(params) {
    return http.get('auth/login', {
      ...params
    });
  },
  getBranchMeet() {
    // TODO format data
    return http.get('ticket/queryByPsnId').then(res => res.result || []).catch(() => []).then(result => result.filter(value => !!value));
  },
  getTicketList() {
    return http.post('mob/queryTicketListByLoginUser').then((res) => {
      if (!res.success) return [];
      return res.result ? [...res.result.myTicketList] : [];
    });
  },
  getTicket(ticketTypeId, ticketId) {
    return http
      // .post('/mob/queryTicketListByUserId', { ticketTypeId, ticketId })
      .post('mob/queryTicketListByTicketId', {
        ticketTypeId: +ticketTypeId,
        ticketId: +ticketId
      })
      .then(res => {
        if (!res.result || !res.result.myTicketList) return {};
        return {
          ...res.result.myTicketList,
          price: res.result.price,
          address: res.result.address,
          etime: res.result.etime,
          stime: res.result.stime,
          activeDate: res.result.activeDate
        };
      });
  },
  getQRCode(activityId) {
    return http.post('verification/genQR', {
      activityId
    }).then(({
      result,
      success
    }) => {
      if (!success) return '';
      return JSON.parse(result);
    });
  },
  getJsConfig() {
    const {
      corpId,
      appKey,
      appId
    } = config.dingtalk;
    return http.get('auth/getJsConfig', {
      corpId,
      appKey,
      appId
    });
  },
  getDeadlineTime(id) {
    return http.get('branchTicket/queryyunxiBookingCutofftime', {
      projectId: id
    }).then(res => {
      if (!res.result.cutoffTime) {
        res.result.cutoffTime = new Date().getTime();
      } else {
        const dateArray = (res.result.cutoffTime.split(' ')[0] || '').split('-');
        res.result.cutoffTime = new Date(Number(dateArray[0]), Number(dateArray[1], Number(dateArray[2]))).getTime();
      }
      return res.result;
    });
  },
  async isNeedUploadFace(activityId) {
    const {
      result,
      success
    } = await http.get('userFaceInfo/getPhotoStatus', {
      activityId
    });

    if (!success) return false;

    return result;
  },
  uploadFace(activityId, picUrl) {
    return http.post('userFaceInfo/enterFace', {
      activityId: Number(activityId),
      picUrl
    }).then((res) => {
      if (!res.success) return Promise.reject(res.errorMsg);
    }).catch((reason) => Promise.reject(typeof reason === 'string' ? reason : '服务器开小差啦'));
  }
};
